import __builtin__
from panda3d.core import Texture, TextureStage, Vec3, BitMask32
from panda3d.bullet import BulletPlaneShape, BulletBoxShape
from panda3d.bullet import BulletRigidBodyNode, BulletGhostNode
from pandac.PandaModules import TransparencyAttrib
from panda3d.core import PointLight, Spotlight, AmbientLight, DirectionalLight, LightAttrib, Vec4, Vec3, VBase4, Point3


def createBullet():
    mainNode = __builtin__.render.attachNewNode("BulletNode")

    tower = loadBlankModel("planet_sphere", "tower.png")
    tower.setScale(0.1, 0.1, 0.1)
    tower.setTexScale(TextureStage.getDefault(), 1, 1)
    tower.flattenLight()
    tower.reparentTo(mainNode)

    return mainNode


def createTower():
    mainNode = __builtin__.render.attachNewNode("TowerNode")

    tower = loadBlankModel("rook", "tower.png")
    tower.setTexScale(TextureStage.getDefault(), 2, 2)
    tower.setScale(2,2,2)
    tower.flattenLight()
    tower.reparentTo(mainNode)

    ghost = BulletGhostNode('TowerGhost')

    mainNode.attachNewNode(ghost)
    mainNode.setCollideMask(BitMask32(0x0f))
    ghost.addShape(BulletBoxShape(Vec3(0.25, 0.25, 0.5)))
    __builtin__.base.physicWorld.attachGhost(ghost)

    plight = PointLight('plight')
    plight.setColor(VBase4(0.5, 0.5, 0.5, 1))
    plnp = mainNode.attachNewNode(plight)
    plnp.setName("light")
    plnp.setPos(0, -1, 0)
    #plight.setShadowCaster(True, 512, 512)
    __builtin__.base.render.setLight(plnp)
    plight.setAttenuation(Point3(0, 0, 0.5))

    return mainNode


def createCrosshair():
    crosshair = loadBlankModel("plane", "crosshair.png")
    crosshair.setTexScale(TextureStage.getDefault(), 1, 1)
    crosshair.setP(-90)
    crosshair.setScale(1)
    crosshair.setTransparency(TransparencyAttrib.MAlpha)

    plight = PointLight('plight')
    plight.setColor(VBase4(0.5, 0.5, 0.5, 1))
    plnp = crosshair.attachNewNode(plight)
    plnp.setName("light")
    plnp.setPos(0, -1, 0)
    #plight.setShadowCaster(True, 512, 512)
    __builtin__.base.render.setLight(plnp)
    plight.setAttenuation(Point3(0, 0, 0.5))

    return crosshair


def createSelection():
    sel = loadBlankModel("plane", "selection.png")
    sel.setTexScale(TextureStage.getDefault(), 1, 1)
    sel.setP(-90)
    sel.setScale(1)
    sel.setTransparency(TransparencyAttrib.MAlpha)

    return sel


def createBackground():
    bg = loadBlankModel("plane", "grass01.jpg")
    bg.flattenLight()
    bg.setTexScale(TextureStage.getDefault(), 5, 5)
    bg.setScale(55)
    bg.setP(-90)

    bgShape = BulletPlaneShape(Vec3(0, 0, 1), 0)
    bgNode = BulletRigidBodyNode("Ground")
    bgNode.addShape(bgShape)
    __builtin__.base.physicWorld.attachRigidBody(bgNode)

    return bg


def loadBlankModel(modelName, texName):
    model = __builtin__.loader.loadModel("client/models/" + modelName)
    model.reparentTo(__builtin__.render)
    tex = __builtin__.loader.loadTexture("client/textures/" + texName)  # Load the texture
    tex.setWrapU(Texture.WM_repeat)
    tex.setWrapV(Texture.WM_repeat)
    model.setTexture(tex, 1)  # Set the texture

    return model
